﻿using System;
using System.Security.Cryptography;
using System.Windows.Media.Imaging;
using Brjnk.Maps;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MapsTest
{
    [TestClass()]
    public class TileGeneratorTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion Additional test attributes

        [TestMethod()]
        public void GenerateTest()
        {
            TileGeneratorArgs args = TileGeneratorArgs.GetGreen();
            args.Header = "Header";
            args.Text = "Text";
            int pixelWidth = 256;
            int pixelHeight = 256;
            double dpi = 96;
            var actual = TileGenerator.Generate(args, pixelWidth, pixelHeight, dpi);
            Assert.AreEqual(256, actual.PixelWidth);
            Assert.AreEqual(256, actual.PixelHeight);
            Assert.AreEqual(256, actual.Width);
            Assert.AreEqual(256, actual.Height);
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            var ms = new System.IO.MemoryStream();
            encoder.Frames.Add(BitmapFrame.Create(actual));
            encoder.Save(ms);
            using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
            {
                string hash = Convert.ToBase64String(sha1.ComputeHash(ms.ToArray()));
                System.Diagnostics.Debug.WriteLine("Hash vygenerované dalždice: " + hash);
                Assert.AreEqual("MxQ0b0UIoNTz2ObOovWYlR56KzM=", hash, "Vygenerovaná dlaždice byla změněna, překontrolujte její správnost");

                // System.IO.File.Create("C:\\generatedTile.png").Write(ms.ToArray(),0,(int) ms.Length);
            }
        }
    }
}